#!/usr/bin/osascript
-- Script d'installation optimisé pour LCN Assist avec Platypus (mode root)
-- Avec barre de progression, raccourci sur le Bureau et lancement automatique

on run
    -- Configuration
    set armUrl to "https://support.lacuveenumerique.fr/download/osx/LCN39-aarch64.dmg"
    set intelUrl to "https://support.lacuveenumerique.fr/download/osx/LCN39-x86_64.dmg"
    set appName to "LCN Assist.app"
    set desktopShortcut to true  -- Créer un raccourci sur le Bureau
    set autoLaunch to true      -- Lancer l'application après installation

    -- 1. Détecte l'architecture
    display notification "Détection de l'architecture..." with title "Installation LCN Assist"
    set arch to do shell script "uname -m"
    if arch is "arm64" then
        set dmgUrl to armUrl
        set archHuman to "Apple Silicon (ARM64)"
    else if arch is "x86_64" then
        set dmgUrl to intelUrl
        set archHuman to "Intel (x86_64)"
    else
        display alert "Erreur" message "Architecture non supportée: " & arch
        return
    end if

    -- 2. Crée un dossier temporaire
    set workDir to do shell script "mktemp -d /tmp/lcn_install.XXXXXX"
    set pkgPath to workDir & "/" & (do shell script "basename " & quoted form of dmgUrl)

    try
        -- 3. Téléchargement (20%)
        display notification "Téléchargement du DMG (" & archHuman & ")..." with title "Installation LCN Assist"
        do shell script "curl -fL " & quoted form of dmgUrl & " -o " & quoted form of pkgPath

        -- 4. Montage du DMG (40%)
        display notification "Montage du DMG..." with title "Installation LCN Assist"
        do shell script "hdiutil attach " & quoted form of pkgPath & " -nobrowse -noverify -noautoopen"
        -- On récupère le point de montage via hdiutil info
        set mntDir to (do shell script "hdiutil info -plist | grep -A 2 'mount-point' | grep '<string>' | sed -E 's/.*<string>([^<]*)<\\/string>.*/\\1/' | tail -1")

        if mntDir is "" then
            error "Impossible de déterminer le point de montage du DMG."
        end if

        -- 5. Copie de l'application (60%)
        set srcApp to do shell script "find " & quoted form of mntDir & " -iname '*.app' -maxdepth 2 | head -1"
        if srcApp is "" then
            error "Aucune application trouvée dans le DMG. Point de montage : " & mntDir
        end if

        set dstApp to "/Applications/" & appName

        -- Supprime l'ancienne version
        try
            do shell script "rm -rf " & quoted form of dstApp
        end try

        display notification "Copie de l'application dans /Applications..." with title "Installation LCN Assist"
        do shell script "cp -R " & quoted form of srcApp & " " & quoted form of dstApp

        -- 6. Configuration (80%)
        display notification "Configuration finale..." with title "Installation LCN Assist"

        -- Corrige les permissions
        set binName to do shell script "defaults read " & quoted form of dstApp & "/Contents/Info CFBundleExecutable"
        set binPath to dstApp & "/Contents/MacOS/" & binName

        do shell script "chmod +x " & quoted form of binPath
        do shell script "xattr -dr com.apple.quarantine " & quoted form of dstApp
        do shell script "codesign --force --deep --sign - " & quoted form of dstApp

        -- 7. Créer un raccourci sur le Bureau
        if desktopShortcut then
            display notification "Création d'un raccourci sur le Bureau..." with title "Installation LCN Assist"
            set desktopPath to (path to desktop folder) as text
            do shell script "ln -s " & quoted form of dstApp & " " & quoted form of (desktopPath & appName) & " 2>/dev/null || cp -R " & quoted form of dstApp & " " & quoted form of (desktopPath & appName)
        end if

        -- 8. Lancement automatique
        if autoLaunch then
            display notification "Lancement de l'application..." with title "Installation LCN Assist"
            delay 1
            tell application dstApp to activate
        end if

        -- 9. Nettoyage
        do shell script "hdiutil detach " & quoted form of mntDir & " >/dev/null 2>&1"
        do shell script "rm -rf " & quoted form of workDir

        -- Notification finale
        display notification "Installation terminée avec succès !" with title "LCN Assist"

    on error errMsg
        try
            do shell script "hdiutil detach " & quoted form of mntDir & " >/dev/null 2>&1"
            do shell script "rm -rf " & quoted form of workDir
        end try
        display alert "Erreur" message errMsg
    end try
end run
